/**
 * @author John Kelleher
 *
 */
/* jQuery functions to initialize after the DOM is loaded */
/* but before the page has finished rendering. */
/* $(document).ready() occurs as soon as the DOM is loaded */
$(document).ready(function(){
});

$(window).load(function(){
    initEventHandlers();
});


function initEventHandlers(){
    $('#WS-primaryNav ul li a').mouseenter(function(){
        spin($(this), 120, 0, 50, 0, 0);
    });
    
    $('#WS-secondaryNav ul li a').mouseenter(function(){
        spin($(this), 120, 0, -38, 142, 0);
    });
    
    $('#WS-minimizeCss-bt').click(function(){
        var cssInput = $('#WS-cssInput-ta').val();
        var cssOutput = cssInput;
        
        
        // Remove comments.
        if ($('#WS-RemoveComments-cb').is(':checked')) {
            cssOutput = cssOutput.replace(/\/\*(.|[\r\n])*?\*\//gi, "");
        }
        
        // Remove line breaks.
        if ($('#WS-RemoveLineBreaks-cb').is(':checked')) {
            cssOutput = cssOutput.replace(/([\r\n])/g, " ");
        }
        
        // Replace muliple blank spaces with one blank space.
        if ($('#WS-RemoveMultipleBlankSpaces-cb').is(':checked')) {
            cssOutput = cssOutput.replace(/ +/g, " ");
        }
        
        // Remove blank space around css syntax.
        if ($('#WS-RemoveBlankSpaceCssSyntax-cb').is(':checked')) {
            cssOutput = cssOutput.replace(/{ /g, "{");
            cssOutput = cssOutput.replace(/ {/g, "{");
            
            cssOutput = cssOutput.replace(/} /g, "}");
            cssOutput = cssOutput.replace(/ }/g, "}");
            
            cssOutput = cssOutput.replace(/: /g, ":");
            cssOutput = cssOutput.replace(/ :/g, ":");
            
            cssOutput = cssOutput.replace(/; /g, ";");
            cssOutput = cssOutput.replace(/ ;/g, ";");

            cssOutput = cssOutput.replace(/, /g, ",");
            cssOutput = cssOutput.replace(/ ,/g, ",");
        }
        
				// Remove last semi-colon.
        if ($('#WS-RemoveLastSemicolon-cb').is(':checked')) {
            cssOutput = cssOutput.replace(/;}/g, "}");
        }
        
        // Minize colors.
        if ($('#WS-MinimizeColours-cb').is(':checked')) {
            cssOutput = cssOutput.replace(/#000000/gi, "#000");
            cssOutput = cssOutput.replace(/#111111/gi, "#111");
            cssOutput = cssOutput.replace(/#222222/gi, "#222");
            cssOutput = cssOutput.replace(/#333333/gi, "#333");
            cssOutput = cssOutput.replace(/#444444/gi, "#444");
            cssOutput = cssOutput.replace(/#555555/gi, "#555");
            cssOutput = cssOutput.replace(/#666666/gi, "#666");
            cssOutput = cssOutput.replace(/#777777/gi, "#777");
            cssOutput = cssOutput.replace(/#888888/gi, "#888");
            cssOutput = cssOutput.replace(/#999999/gi, "#999");
            cssOutput = cssOutput.replace(/#aaaaaa/gi, "#aaa");
            cssOutput = cssOutput.replace(/#bbbbbb/gi, "#bbb");
            cssOutput = cssOutput.replace(/#cccccc/gi, "#ccc");
            cssOutput = cssOutput.replace(/#dddddd/gi, "#ddd");
            cssOutput = cssOutput.replace(/#eeeeee/gi, "#eee");
            cssOutput = cssOutput.replace(/#ffffff/gi, "#fff");
        }
        
        $('#WS-cssOutput-ta').val(cssOutput);
    });
    
    $('#WS-clearOutput-bt').click(function(){
    
        $('#WS-cssOutput-ta').val("");
    });
    
}

function spin(obj, time, offsetX, offsetY, startX, startY){


    var changeText = false;
    var text = "";
    
    if (!isInteger()) {
    
    }
    
    if (obj.text() !== "") {
        changeText = true;
        text = obj.text();
        obj.text("");
    }
    
    var x = startX + offsetX;
    var y = startY + offsetY;
    
    obj.animate({
        backgroundPosition: ((x) + "px " + (y) + "px")
    }, time).animate({
        backgroundPosition: ((x + offsetX) + "px " + (y + offsetY) + "px")
    }, time).animate({
        backgroundPosition: ((x + (2 * offsetX)) + "px " + (y + (2 * offsetY)) + "px")
    }, time, function(){
        if (changeText) {
            obj.text(text);
        }
    }).animate({
        backgroundPosition: (startX + "px " + startY + "px")
    }, time);
}

function isInteger(){

}

function replaceWhiteSpace(){


}
